<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理系统</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/my_css.css">
</head>

<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="my-logo">订单管理系统</div>
        </div>
    </div>

    <div class="my-signin-form">
        <form class="layui-form" style="text-align: center">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="username" name="username" placeholder="请输入用户名"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="password" name="password" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="signin">登录</button>
                        <button class="layui-btn" lay-submit lay-filter="signup">注册</button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
        layui.use('form', function() {
            let form = layui.form;

            form.on('submit(signin)', function(data) {
                let username = data.field.username;
                let password = data.field.password;

                if (username.length == 0 || password.length == 0) {
                    layer.msg('必填项不能为空!', {
                        icon: 5,
                        time: 2000
                    });
                    return false;
                }

                $.ajax({
                    url: 'sign_in',
                    data: {
                        'username': username,
                        'password': password
                    },
                    method: 'post',
                    success: function (res) {
                        $("#username").val('');
                        $("#password").val('');
                        if (res) {
                            window.location.href = 'index.html';
                        } else {
                            layer.msg('用户名或密码错误!', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function () {
                        layer.msg('登录错误!', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                return false;
            });

            form.on('submit(signup)', function () {
                $("#username").val('');
                $("#password").val('');
                window.location.href = 'signup.html';
                return false;
            });
        });
    </script>
</body>
</html>